package com.interesting.client.support.file;

import java.io.File;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.interesting.client.UI.commonActivity.UIUtil;

/**
 * User: Jiang Qi Date: 12-8-3
 */
public class ImageTool {

	public static String getLargePictureWithoutRoundedCorner(String url,
			FileDownloaderHttpHelper.DownloadListener downloadListener, FileLocationMethod fileLocationMethod) {

		String absoluteFilePath = FileManager.getFilePathFromUrl(url, fileLocationMethod);

		File file = new File(absoluteFilePath);

		if (file.exists()) {
			return absoluteFilePath;

		} else {

			getBitmapFromNetWork(url, absoluteFilePath, downloadListener);

			if (isThisBitmapCanRead(absoluteFilePath)) {
				return absoluteFilePath;
			} else {
				return "about:blank";
			}

		}

	}

	public static boolean isThisBitmapCanRead(String path) {
		File file = new File(path);

		if (!file.exists()) {
			return false;
		}

		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);
		int width = options.outWidth;
		int height = options.outHeight;
		if (width == -1 || height == -1) {
			return false;
		}

		return true;
	}

	public static int[] getBitmapSize(String path) {
		int[] result = { -1, -1 };
		File file = new File(path);

		if (!file.exists()) {
			return result;
		}

		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);
		int width = options.outWidth;
		int height = options.outHeight;
		if (width > 0 && height > 0) {
			result[0] = width;
			result[1] = height;
		}

		return result;
	}

	public static Bitmap decodeBitmapFromSDCard(String path, int reqWidth, int reqHeight) {

		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

		options.inJustDecodeBounds = false;

		return BitmapFactory.decodeFile(path, options);

	}

	public static Bitmap getNotificationSendFailedPic(String path) {

		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		options.inSampleSize = calculateInSampleSize(options, UIUtil.getScreenWidth(), UIUtil.getScreenHeight());

		options.inJustDecodeBounds = false;

		return BitmapFactory.decodeFile(path, options);

	}

	public static boolean getBitmapFromNetWork(String url, String path,
			FileDownloaderHttpHelper.DownloadListener downloadListener) {
		for (int i = 0; i < 3; i++) {
			if (HttpUtility.getInstance().executeDownloadTask(url, path, downloadListener)) {
				return true;
			}
			new File(path).delete();
		}

		return false;
	}

	private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (height > reqHeight && reqHeight != 0) {
				inSampleSize = (int) Math.floor((double) height / (double) reqHeight);
			}

			int tmp = 0;

			if (width > reqWidth && reqWidth != 0) {
				tmp = (int) Math.floor((double) width / (double) reqWidth);
			}

			inSampleSize = Math.max(inSampleSize, tmp);

		}
		int roundedSize;
		if (inSampleSize <= 8) {
			roundedSize = 1;
			while (roundedSize < inSampleSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (inSampleSize + 7) / 8 * 8;
		}

		return roundedSize;
	}

}
